﻿package mortal.game.view.platform
{
    import Message.DB.Tables.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.yyZiZuan.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class PlatformDownLoadModule extends BaseWindow
    {
        private var _bg:GImageBitmap;
        private var _downLoadButton:GLoadingButton;
        private var _getGiftBtn:GLoadingButton;
        private var _giftList:GTileList;
        public var platformData:PlatformDownLoadShowData;
        public var clientType:int = 2;
        private var isCreate:Boolean = false;

        public function PlatformDownLoadModule(param1:ILayer = null)
        {
            super(param1);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this.disposeChild(param1);
            super.disposeImpl(param1);
            return;
        }// end function

        private function disposeChild(param1:Boolean = true) : void
        {
            this.isCreate = false;
            if (this._bg)
            {
                this._bg.dispose(param1);
                this._bg = null;
            }
            if (this._downLoadButton)
            {
                this._downLoadButton.dispose(param1);
                this._downLoadButton = null;
            }
            if (this._getGiftBtn)
            {
                this._getGiftBtn.dispose(param1);
                this._getGiftBtn = null;
            }
            if (this._giftList)
            {
                this._giftList.dispose(param1);
                this._giftList = null;
            }
            return;
        }// end function

        protected function onClickHandle(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            if (event.target == this._downLoadButton)
            {
                if (this.clientType == PlatformDownLoadUtil.clientType1)
                {
                    DownLoadFileUtil.downloadToDesktop(ParamsConst.instance.platformDownLoadUrl, this.downloadCallBack);
                }
                else
                {
                    DownLoadFileUtil.downloadToDesktop(ParamsConst.instance.platformDownLoadUrl2, this.downloadCallBack);
                }
            }
            else if (event.target == this._getGiftBtn)
            {
                if (this.clientType == PlatformDownLoadUtil.clientType1)
                {
                    if (ParamsConst.instance.client == PlatformDownLoadUtil.clientType1)
                    {
                        _loc_2 = PlatformDownLoadUtil.getExtends();
                        _loc_3 = PlatformGiftConfig.instance.getGift(_loc_2).id;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.PlatformRequestDownLoadGift, _loc_3));
                    }
                    else
                    {
                        MsgManager.showRollTipsMsg("用微端登录才可以领取礼包");
                    }
                }
                else if (ParamsConst.instance.client == PlatformDownLoadUtil.clientType2)
                {
                    _loc_2 = PlatformDownLoadUtil.getExtends(PlatformDownLoadUtil.clientType2);
                    _loc_3 = PlatformGiftConfig.instance.getGift(_loc_2).id;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PlatformRequestDownLoadGift2, _loc_3));
                }
                else
                {
                    MsgManager.showRollTipsMsg("用输入法登录才可以领取礼包");
                }
            }
            return;
        }// end function

        protected function downloadCallBack() : void
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            if (!this.isCreate)
            {
                this.createChild();
            }
            return;
        }// end function

        private function createChild() : void
        {
            var _loc_1:int = 0;
            var _loc_2:TPlatformGift = null;
            if (this.platformData)
            {
                this.isCreate = true;
                setSize(this.platformData.size.x, this.platformData.size.y);
                title = this.platformData.title;
                this._bg = UIFactory.gImageBitmap(this.platformData.bgResName, this.platformData.bgPos.x, this.platformData.bgPos.y, this);
                this._downLoadButton = UIFactory.gLoadingButton(this.platformData.downLoadBtnName, this.platformData.downLoadBtnPos.x, this.platformData.downLoadBtnPos.y, this.platformData.downLoadBtnSize.x, this.platformData.downLoadBtnSize.y, this);
                this._downLoadButton.configEventListener(MouseEvent.CLICK, this.onClickHandle);
                this._getGiftBtn = UIFactory.gLoadingButton(this.platformData.getGiftBtnName, this.platformData.getGiftBtnPos.x, this.platformData.getGiftBtnPos.y, this.platformData.getGiftBtnSize.x, this.platformData.getGiftBtnSize.y, this);
                this._getGiftBtn.configEventListener(MouseEvent.CLICK, this.onClickHandle);
                this._giftList = UIFactory.tileList(this.platformData.giftListPos.x, this.platformData.giftListPos.y, 500, 80, this);
                this._giftList.columnWidth = this.platformData.cellSize.x;
                this._giftList.rowHeight = this.platformData.cellSize.y;
                this._giftList.setStyle("cellRenderer", this.platformData.cellRender);
                this._giftList.direction = GBoxDirection.HORIZONTAL;
                _loc_1 = PlatformDownLoadUtil.getExtends(this.clientType);
                _loc_2 = PlatformGiftConfig.instance.getGift(_loc_1);
                if (_loc_2)
                {
                    this._giftList.dataProvider = this.getGiftDataProviderByGiftItemCode(_loc_2.reward);
                    this._giftList.x = this._giftList.x - 0.5 * (this._giftList.dataProvider.length * this._giftList.columnWidth);
                    this._giftList.drawNow();
                }
            }
            return;
        }// end function

        private function getGiftDataProviderByGiftItemCode(param1:int) : DataProvider
        {
            var _loc_5:Array = null;
            var _loc_6:ItemData = null;
            var _loc_9:YYZiZuanGiftDataGroup = null;
            var _loc_2:* = new DataProvider();
            var _loc_3:* = new ItemData(param1).itemInfo as ItemGiftBagInfo;
            var _loc_4:* = _loc_3.itemList1.split("&");
            var _loc_7:* = _loc_4.length;
            var _loc_8:int = 0;
            while (_loc_8 < _loc_4.length)
            {
                
                _loc_5 = _loc_4[_loc_8].split("#");
                _loc_9 = new YYZiZuanGiftDataGroup();
                _loc_9.itemCode = int(_loc_5[1]);
                _loc_9.itemAmount = _loc_5[2];
                _loc_9.isBind = true;
                _loc_2.addItem(_loc_9);
                _loc_8++;
            }
            return _loc_2;
        }// end function

        public function updateShow() : void
        {
            if (this.isCreate)
            {
                this.disposeChild();
            }
            this.createChild();
            return;
        }// end function

    }
}
